package com.dk.springmvc.restful.controller;

import com.dk.springmvc.restful.entity.Emp;
import com.dk.springmvc.restful.service.EmpService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmpController {
    private final EmpService empService;

    public EmpController(EmpService empService) {
        this.empService = empService;
    }

    @GetMapping("/emp")
    public ModelAndView getAllEmp(){
        ModelAndView modelAndView = new ModelAndView("empList");
        modelAndView.addObject("allEmp",empService.getAllEmp());
        return modelAndView;
    }

    @GetMapping("/emp/{empId}")
    public ModelAndView getEmp(@PathVariable Integer empId){
        ModelAndView modelAndView = new ModelAndView("empInfo");
        modelAndView.addObject("emp",empService.getEmp(empId));
        return modelAndView;
    }

    @PostMapping("/emp")
    public ModelAndView addEmp(Emp emp){
        empService.addEmp(emp);
        ModelAndView modelAndView = new ModelAndView("successful");
        modelAndView.addObject("msg","用户"+ emp.toString() +"添加成功");
        return modelAndView;
    }

    @PutMapping("/emp")
    public ModelAndView modifyEmp(Emp emp){
        empService.modifyEmp(emp);
        ModelAndView modelAndView = new ModelAndView("successful");
        modelAndView.addObject("msg","用户"+ emp.toString() +"修改成功");
        return modelAndView;
    }

    @DeleteMapping("/emp/{empId}")
    public ModelAndView removeEmp(@PathVariable Integer empId){
        empService.removeEmp(empId);
        ModelAndView modelAndView = new ModelAndView("successful");
        modelAndView.addObject("msg","用户"+ empId +"删除成功");
        return modelAndView;
    }

    @GetMapping("/forwardAddEmp")
    public String forwardAddEmp(){
        return "empAdd";
    }
}
